package com.example.news_demo.controller;

import com.example.news_demo.entity.Article;
import com.example.news_demo.entity.User;
import com.example.news_demo.service.ArticleService;
import com.example.news_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/User")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    ArticleService articleService;

    @RequestMapping("/Login")
    public String login(Model model,HttpServletRequest request){
        List<User> users = userService.getAll();
        model.addAttribute("users",users);
        return "login";
    }
    @RequestMapping("/Login2")
    public String login2(Model model,HttpServletRequest request){

        int id = Integer.parseInt(request.getParameter("id"));
        User user = userService.getOneById(id);
        HttpSession session=request.getSession();//获取session并将userName存入session对象y
        session.setAttribute("loginUser", user);
        List<Article> articleList = articleService.getInterest(user.getId());
        model.addAttribute("article",articleList);
        List<Article> randomList = articleService.getRandom(10);
        model.addAttribute("randomList",randomList);
        return "index";
    }

    @RequestMapping("/userLogin")
    public String userLogin(Model model,@RequestParam(value = "username") String username,@RequestParam(value = "password") String password, HttpServletRequest request){
        if(userService.checkLogin(username,password)){
            User user = userService.getOne(username);
            HttpSession session=request.getSession();//获取session并将userName存入session对象y
            session.setAttribute("loginUser", user);
            List<Article> articleList = articleService.getInterest(user.getId());
            model.addAttribute("article",articleList);
            List<Article> randomList = articleService.getRandom(10);
            model.addAttribute("randomList",randomList);
            return "index";
        }
        return "loginFail";
    }
    @RequestMapping("/logout")
    public String userLogout(HttpServletRequest request, Model model){
        HttpSession session=request.getSession();//获取session并将userName存入session对象y
        session.removeAttribute("loginUser");
        List<Article> randomList = articleService.getRandom(10);
        List<Article> articleList = articleService.getRandom(10);
        model.addAttribute("article",articleList);
        model.addAttribute("randomList",randomList);
        return "index";
    }

}
